package org.njo.webapp.root.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ActionRoleMappingDAO {

    /**
     * 数据库连接对象.
     */
    private Connection connection = null;

    /**
     * 构造函数.<br>
     * 需要一个数据库连接对象作为参数.
     * 
     * @param argConnection
     *            数据库连接对象
     */
    public ActionRoleMappingDAO(Connection argConnection) {
        connection = argConnection;
    }

    /**
     * 从ACTION_ROLE_MAPPING表,读取数据.
     * 
     * @param argStartIndex
     * @param argEndIndex
     * @return
     * @throws java.sql.SQLException
     */
    public List selectActionPaths(int argStartIndex, int argEndIndex)
            throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        List resultList = new ArrayList();
        int curIndex = 0;
        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ACTION_PATH  ");
            selectSql.append(" FROM ACTION_ROLE_MAPPING ");
            selectSql.append(" GROUP BY ACTION_PATH ");

            selectStatement = connection.prepareStatement(selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            resultSet = selectStatement.executeQuery();
            if (resultSet.absolute(argStartIndex)) {
                curIndex = argStartIndex;
                do {
                    String actionPath = resultSet.getString(1);
                    resultList.add(actionPath);
                    curIndex++;
                } while (resultSet.next() && (curIndex <= argEndIndex));
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return resultList;
    }

    /**
     * 从ACTION_ROLE_MAPPING表,根据actionpath读取role.
     * 
     * @param argStartIndex
     * @param argEndIndex
     * @return
     * @throws java.sql.SQLException
     */
    public List selectActionRoles(String argActionPath) throws SQLException {
        StringBuffer selectSql = null;
        PreparedStatement selectStatement = null;
        ResultSet resultSet = null;
        List resultList = new ArrayList();
        int curIndex = 0;

        int argStartIndex = 1;
        int argEndIndex = Integer.MAX_VALUE;

        try {
            selectSql = new StringBuffer();
            selectSql.append(" SELECT ROLE_NAME  ");
            selectSql.append(" FROM ACTION_ROLE_MAPPING ");
            selectSql.append(" WHERE ACTION_PATH = ? ");

            selectStatement = connection.prepareStatement(selectSql.toString(),
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

            selectStatement.setString(1, argActionPath);

            resultSet = selectStatement.executeQuery();
            if (resultSet.absolute(argStartIndex)) {
                curIndex = argStartIndex;
                do {
                    String actionRole = resultSet.getString(1);
                    resultList.add(actionRole);
                    curIndex++;
                } while (resultSet.next() && (curIndex <= argEndIndex));
            }
        } finally {
            if (resultSet != null) {
                resultSet.close();
                resultSet = null;
            }
            if (selectStatement != null) {
                selectStatement.close();
                selectStatement = null;
            }
        }
        return resultList;
    }

}
